package com.atguigu.tingshu.payment.strategy.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.payment.config.WxPayV3Config;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.atguigu.tingshu.payment.strategy.PaySubmitStrategy;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.payment.WeChatPayVo;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.springframework.beans.factory.annotation.Autowired;

public class OrderPaySubmitStrategy implements PaySubmitStrategy {


    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient ;

    @Autowired
    private RSAAutoCertificateConfig autoCertificateConfig ;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient ;

    @Autowired
    private WxPayV3Config wxPayV3Config  ;

    @Autowired
    private PaymentInfoService paymentInfoService ;

    @Override
    public WeChatPayVo paySbumit(String paymentType, String orderNo) {
        //提交下单请求之前进行状态的检验 判断是够过期
        Result<OrderInfo> orderInfoByOrderNo = orderInfoFeignClient.findOrderInfoByOrderNo(orderNo);
        OrderInfo orderInfo = orderInfoByOrderNo.getData();
        if(SystemConstant.ORDER_STATUS_CANCEL.equals(orderInfo.getOrderStatus())){
            throw new GuiguException(ResultCodeEnum.ORDER_PAYE_TIME_OUT) ;
        }

        // 保存微信支付的交易信息
        paymentInfoService.savePaymentInfo(orderInfo) ;

        //没有过期  使用微信支付平台的请求Jsapi的下单接口,生成预交易订单
        WeChatPayVo weChatPayVo = this.requestJsapi(autoCertificateConfig, 1, wxPayV3Config, orderNo, userInfoFeignClient, orderInfo.getOrderTitle());


        return weChatPayVo;
    }

    @Override
    public String getPaymentType() {
        return SystemConstant.PAYMENT_TYPE_ORDER; //订单支付
    }
}
